<?php
/**
 * SiteController is an instance of Controller (application.components). 
 *
 * Controller is an instance of CController (system.web).
 * CController manages a set of actions which deal with the corresponding user requests. 
 * Through the actions, CController coordinates the data flow between models and views. 
 *
 * Eduow :  International system for education (http://eduow.com)
 * Copyright 2010, Nikolay Stoitsev 
 *
 * Licensed under GNU General Public License version 2
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright     Copyright 2010
 * @link          http://eduow.com Eduow Project
 * @package       eduow
 * @subpackage    application.controllers
 * @inheritance   class SiteController » Controller » CController » CBaseController » CComponent
 * @implements    
 * @since         Eduow v 0.0.1
 * @license       http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License version 2
 */

class SiteController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions() {
		return array(
			// captcha action renders the CAPTCHA image
			// this is used by the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xEBF4FB,
			),
		);
	}

	/**
	 * This is the default 'error' action that is invoked
	 * when an error ocure. It dispalays error page and logs 
	 * the error.
	 */
	public function actionError()
	{
    		if($error=Yii::app()->errorHandler->error){
        	$this->render('error', array('error'=>$error));
		}
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$app = Yii::app();
		if (isset($_POST['lang']))
		{
		    switch ($_POST['lang']) {
			case 'bg':
			case 'en':
			case 'ar':
			case 'zh':
			case 'ru':
			case 'es':
			case 'de':
			case 'fr':
			case 'cs':
			case 'da':
			case 'el':
			case 'eo':
			case 'et':
			case 'fi':
			case 'ga':
			case 'hi':
			case 'hu':
			case 'ja':
			case 'lv':
			case 'lt':
			case 'mt':
			case 'nl':
			case 'pl':
			case 'pt':
			case 'ro':
			case 'sk':
			case 'sl':
			case 'sv':
			  $app->language = $_POST['lang'];
			  break;
			default:
			  $app->language = 'bg';
		    } 
		    $app->session['lang'] = $app->language;
		}
		else if (isset($app->session['lang']))
		{
		    $app->language = $app->session['lang'];
		}

		if(!Yii::app()->user->isGuest){
			$dataProvider=new CActiveDataProvider('News',array(
			'criteria'=>array(
				'order'=>'id DESC',
				),
	    		'pagination'=>array(
	       			'pageSize'=>40,
	    			),
			));
		}
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('index',array('dataProvider'=>$dataProvider));
	}

	public function actionTeam()
	{
		// renders the view file 'protected/views/site/team.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('team');
	}

	public function actionEduow()
	{
		// renders the view file 'protected/views/site/eduow.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('eduow');
	}

	public function actionAbout()
	{
		// renders the view file 'protected/views/site/about.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('adout');
	}

	public function actionApi()
	{
		// renders the view file 'protected/views/site/api.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('api');
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$contact=new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$contact->attributes=$_POST['ContactForm'];
			if($contact->validate())
			{
				Yii::app()->getModule('email');
				$email = new Email;
				$email->to = Yii::app()->params['adminEmail'];
				$email->from = $email->replyTo = $contact->email;
				$email->subject = $contact->subject;
				$email->message = $contact->body;
				$email->send();
				
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('contact'=>$contact));
	}

	/**
	 * Create RSS feed
	 */
	public function actionRSS()
    	{
        Yii::import('application.vendors.*');
        require_once('Zend/Feed.php');
        require_once('Zend/Feed/Rss.php');

        // retrieve the latest 20 posts
        $posts=News::model()->findAll(array(
        'order'=>'create_time DESC',
        'limit'=>20,
            ));
        // convert to the format needed by Zend_Feed
        $entries=array();
        foreach($posts as $post)
        {
            $entries[]=array(
            'title'=>$post->title,
            'link'=>CHtml::encode($this->createAbsoluteUrl('post/show',array('id'=>$post->id))),
            'description'=>$post->content,
            'lastUpdate'=>$post->create_time ,
            );
        }
        // generate and render RSS feed
        $feed=Zend_Feed::importArray(array(
        'title'   => 'Eduow New Feed',
        'link'    => $this->createUrl('/site/RSS'),
        'charset' => 'UTF-8',
        'entries' => $entries,
            ), 'rss');
        $feed->send();
    	}
}
